<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Legend\ImportAction;
use App\Http\Controllers\Controller;
use App\Models\Legend;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class LegendController extends Controller
{
    use HasResourceActions;
    private $name = '江湖榜';

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.list'))
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.detail'))
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.edit'))
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header($this->name)
            ->description(trans('admin.create'))
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Legend);

        $grid->id('ID');
        $grid->name('名称');
        $grid->score('积分');
//        $grid->head_img('头像')->image('', 30, 30);
//        $grid->main_honor('展示荣誉');
//        $grid->show('是否显示')->display(function ($show) {
//            return $show ? "<i class='fa fa-check' />" : "<i class='fa fa-remove' />";
//        });
        $grid->sort('排序');

        // 添加到列表上
        $grid->tools(function (Grid\Tools $tools) {
            $tools->append(new ImportAction());
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Legend::findOrFail($id));

        $show->id('ID');
        $show->name('名称');
        $show->score('积分');
//        $show->head_img('头像')->image();
//        $show->main_honor('展示荣誉');
//        $show->show('是否显示')->unescape()->as(function ($show) {
//            return $show ? "<i class='fa fa-check' />" : "<i class='fa fa-remove' />";
//        });
        $show->sort('排序');
//        $show->description('Description', '描述')->unescape()->as(function ($desc) {
//            return $desc;
//        });

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Legend);

        $form->text('name', '名称')->rules('required');
        $form->number('score', '积分')->default(0)->min(0)->default(0)->rules('required');
//        $form->image('head_img', '头像');
//        $form->text('main_honor', '展示荣誉')->rules('required');
//        $form->switch('show', '是否显示');
        $form->number('sort', '排序')->default(0);
//        $form->editor('description', '描述');

        return $form;
    }
}
